package com.treemon.meetingcore.session

{
	import com.treemon.meetingcore.ExtObj.UserDesc;
	import com.treemon.meetingcore.event.MeetingEvent;
	import com.treemon.meetingcore.event.UserEvent;
	
	import flash.utils.Dictionary;
	
	public class UserManager extends ControlBase
	{
		private var userDir:Dictionary = new Dictionary();
        public function UserManager(aSession: MeetingSession)
        {
            super(aSession);
        }
		override protected function addListener(): void
		{
			//添加事件监听
			_session.addEventListener(MeetingEvent.User, handleMeetingUser);
			_session.addEventListener(MeetingEvent.WBCTRL, handleWbCtrl);
		}
		
		private function handleWbCtrl(aEvent: MeetingEvent):void
		{
			
			var xmlQuery: XML = aEvent.xml.children()[0];
			var ns: Namespace = xmlQuery.namespace();
			var strJID:String = xmlQuery.ns::id;
			var strCtrl:String = ""
			if (xmlQuery.attribute("type")=="accept")
			{

				strCtrl =xmlQuery.attribute("ctl");				
				
			}

			
				
				if (userDir[strJID]!=null)
				{
					UserDesc(userDir[strJID])._ctr= strCtrl;
				
					var userEvgent:UserEvent = new UserEvent(UserEvent.StateChange);
					userEvgent.addUser(userDir[strJID]);
					_session.dispatchEvent(userEvgent);
				}
				
							
			
			
			
		}
		
		private function handleMeetingUser(aEvent: MeetingEvent):void
		{
			var xmlQuery: XML = aEvent.xml.children()[0];
			trace(xmlQuery.toXMLString());
			var ns: Namespace = xmlQuery.namespace();
			
			if (xmlQuery.attribute("type")=="remove")
			{
				var userEvgent:UserEvent = new UserEvent(UserEvent.RemoveUser);
				var userinfo:UserDesc = new UserDesc();
				userinfo._jid = xmlQuery.ns::user[0].@jid;
				userEvgent.addUser(userinfo);
				_session.dispatchEvent(userEvgent);
				if (userDir[userinfo._jid]!=null)
				{
					userDir[userinfo._jid] = null;
					delete userDir[userinfo._jid];
				}
				
				
				return;
			}
			
			var userEvgent:UserEvent = new UserEvent(UserEvent.AddUser);
			for(var j:int=0;j<xmlQuery.ns::user.length();j++)
			{ 
				var userinfo:UserDesc = new UserDesc();
				userinfo._nickName = xmlQuery.ns::user[j].@name;
				userinfo._identity = xmlQuery.ns::user[j].@identity;
				userinfo._jid = xmlQuery.ns::user[j].@jid;
				var strVoice: String = xmlQuery.ns::user[j].@voice;
				
				var strCtr: String = xmlQuery.ns::user[j].@ctl;
				userinfo._mmid = xmlQuery.ns::user[j].ns::mmid;
				if (strVoice == "speaking")
				{
					userinfo._isSpeaking = true;					
					_session.addSpeaker(userinfo._mmid);
				}
				userinfo._ctr = strCtr;
			
				
				trace("========name========="+xmlQuery.ns::user[j].@name);
				userEvgent.addUser(userinfo);
				
				if (userDir[userinfo._jid]==null)
				{
					userDir[userinfo._jid]=userinfo;
				}
			
			}
			
			_session.dispatchEvent(userEvgent);
			
			
			
		
			
		}
		
		
		public function modifiedAudioState(ajid:String,isSpeak:Boolean):void
		{
			if (userDir[ajid]!=null)
			{
				UserDesc(userDir[ajid])._isSpeaking = isSpeak;
				
				var userEvgent:UserEvent = new UserEvent(UserEvent.StateChange);
				userEvgent.addUser(userDir[ajid]);
				_session.dispatchEvent(userEvgent);
				
				
				
			}
			
		}

	}
}